<script>
	import {
		EMClient
	} from "@/EaseIM";
	import {
		emConnectListener,
		emMountGlobalListener
	} from "@/EaseIM/emListener";
	import {
		emConnect
	} from "@/EaseIM/emApis";
	import {
		CONNECT_CALLBACK_TYPE,
		HANDLER_EVENT_NAME
	} from "@/EaseIM/constant";
	import {
		emHandleReconnect
	} from "@/EaseIM/utils";
	import {
		dict,
		getCityList
	} from './lib/service';
	
	export default {
		globalData: {
			dict: {
				saleType: [],
				houseType: [],
				priceUnit: [],
				pricePeriod: [],
				cityList: [],
				systemConfig: {},
			}
		},
		onLaunch: function() {
			let _this = this;
			//传给监听callback回调
			const connectedCallback = (type) => {
				console.log(">>>>>IM连接回调", type);
				if (type === CONNECT_CALLBACK_TYPE.CONNECT_CALLBACK) {
					this.onConnectedSuccess();
				}
				if (type === CONNECT_CALLBACK_TYPE.DISCONNECT_CALLBACK) {
					this.onDisconnect();
				}
				if (type === CONNECT_CALLBACK_TYPE.RECONNECTING_CALLBACK) {
					this.onReconnecting();
				}
			};
			/* 链接所需监听回调 */
			emConnectListener(connectedCallback);
			/* 全局类型监听集合、消息、联系人、群组等... */
			emMountGlobalListener();
			this.handleAutoLoginEaseIM();
			this.handleDict();
			uni.$on('appLaunched', () => {
				console.log('Simulate App Launch');
				_this.handleDict();
			});

		},
		computed: {
			loginStoreStatus() {
				return this.$store.state.LoginStore.loginStatus;
			},
			loginStoreUserBaseInfos() {
				return this.$store.state.LoginStore.loginUserBaseInfos;
			},
		},
		onShow: function() {},
		onHide: function() {},
		methods: {
			onConnectedSuccess() {
				const {
					imuser
				} = this.loginStoreUserBaseInfos || {};
				const finalLoginUserId = imuser || EMClient.user;
				if (!this.loginStoreStatus) {
					this.fetchLoginUserNeedData();
					uni.hideLoading();
					console.log(">>>>>>开始跳转至会话列表页面");
					uni.switchTab({
						url: "/pages/new/message",
					});
				}
				this.$store.commit("SET_LOGIN_USER_BASE_INFOS", {
					imuser: finalLoginUserId,
				});
				this.$store.commit("SET_LOGIN_STATUS", true);
			},
			onDisconnect() {
				const {
					closeEaseIM
				} = emConnect();
				const {
					actionEMReconnect
				} = emHandleReconnect();
				//断开回调触发后，如果业务登录状态为true则说明异常断开需要重新登录
				if (!this.loginStatus) {
					uni.showToast({
						title: "退出登录",
						icon: "none",
						duration: 2000,
					});
					uni.navigateTo({
						url: "/pages/login/login",
					});
					closeEaseIM();
				} else {
					console.log(">>>>>需执行重登逻辑");
					//执行通过token进行重新登录
					actionEMReconnect();
				}
			},
			onReconnecting() {
				uni.showToast({
					title: "IM 重连中...",
					icon: "none",
				});
			},
			//获取登录所需基础参数
			async fetchLoginUserNeedData() {
				await this.$store.dispatch("fetchFriendList");
				//获取好友用户属性
				await this.$store.dispatch("fetchFriendUserInfoCollection");
				//获取当前登录用户好友信息
				await this.$store.dispatch("fetchLoginUserProfile");
				await this.$store.dispatch("fetchBlockUserList");
				// 在线状态订阅
				await this.$store.dispatch("subscribePresenceStatus");
				this.fetchJoinedGroupList();
				//初始化缓存本地的新邀请列表
				// this.$store.commit("INIT_RECEIVE_INVITE_LIST");
			},
			//获取加入的群组列表
			async fetchJoinedGroupList() {
				//获取群组列表
				await this.$store.dispatch("fetchJoinedGroupList", {
					isInit: true,
				});
			},
			//自动登录
			handleAutoLoginEaseIM() {
				const {
					actionEMReconnect
				} = emHandleReconnect();
				const loginInfos = uni.getStorageSync(`EM_LOGIN_INFOS`);
				if (!loginInfos) return;
				actionEMReconnect();
			},
			handleDict() {
				dict({
					lang: this.$store.state.language
				}).then((res) => {
					var data = res.data;
					console.log('获取数据', data)
					this.globalData.dict.saleType = data.filter((item) => item.type == 'houseType').map((item) =>
						({
							text: item.keyLabel,
							value: item.keyValue
						}));
					this.globalData.dict.houseType = data.filter((item) => item.type == 'propertyType').map((
						item) => ({
						text: item.keyLabel,
						value: item.keyValue
					}));
					this.globalData.dict.priceUnit = data.filter((item) => item.type == 'pay_unit_type').map((
						item) => ({
						text: item.keyLabel,
						value: item.keyValue
					}));
					this.globalData.dict.pricePeriod = data.filter((item) => item.type == 'pay_period_type').map((
						item) => ({
						text: item.keyLabel,
						value: item.keyValue
					}));
					data.filter((item) => item.type == '"servicePhone"').map((item) => {
						this.globalData.dict.systemConfig[item.keyName] = item.keyValue;
					});
					console.log(this.globalData.dict.systemConfig)
					getCityList().then((res) => {
						this.globalData.dict.cityList = res.data.map((item) => ({
							text: item.name,
							value: item.id
						}));

						this.$store.dispatch('initDict', this.globalData.dict);
					});
				});
			}
		}
	};
</script>

<style lang="scss">
	@import '@/uni_modules/uview-ui/index.scss';
	@import 'static/index.scss';

	/*每个页面公共css */
	@font-face {
		font-family: inter;
		src: url('./font/Inter-var-2.ttf');
	}

	.u-calendar {
		margin-bottom: 50px !important;
	}

	page {
		background-color: #fff;
		font-family: inter;
	}

	ul {
		padding-left: 0;
	}

	ul,
	li {
		list-style: none;
	}

	view,
	text {
		font-family: inter;
	}

	.shadow {
		box-shadow: 0px 20px 12px rgba(61, 61, 61, 0.01), 0px 9px 9px rgba(61, 61, 61, 0.02), 0px 2px 5px rgba(61, 61, 61, 0.02), 0px 0px 0px rgba(61, 61, 61, 0.02);
		border-radius: 10px;
	}

	.form-input {
		border: 1px solid #e5e5e5;
		border-radius: $uni-border-radius-base;
		font-size: 14px;
		line-height: 38px;
		padding: 0 8px;
		height: 38px;
	}

	.flex-center {
		display: flex;
		align-items: center;
		justify-content: center;
	}

	.row {
		display: flex;
		align-items: center;
	}

	.between {
		justify-content: space-between;
	}

	.around {
		justify-content: space-around;
	}

	.flex-layout-col {
		display: flex;
		flex-flow: column;
	}

	.flex-layout-row {
		display: flex;
	}

	.line1 {
		overflow: hidden;
		text-overflow: ellipsis;
		white-space: nowrap;
	}

	.gap-col {
		margin-top: $uni-spacing-col-base;
		margin-bottom: $uni-spacing-col-base;
	}

	.gap-col-top {
		margin-top: $uni-spacing-col-base;
	}

	.gap-col2 {
		margin-top: $uni-spacing-col-base * 2;
		margin-bottom: $uni-spacing-col-base * 2;
	}

	.gap-col-top2 {
		margin-top: $uni-spacing-col-base * 2;
	}

	.gap-row {
		margin-left: $uni-spacing-row-base;
		margin-right: $uni-spacing-row-base;
	}

	.gap-row-left {
		margin-left: $uni-spacing-row-base;
	}

	.gap-row2 {
		margin-left: $uni-spacing-row-base * 2;
		margin-right: $uni-spacing-row-base * 2;
	}

	.gap-row-left2 {
		margin-left: $uni-spacing-row-base * 2;
	}

	.gap {
		box-sizing: border-box;
		padding: 16px;
	}

	.content {
		box-sizing: border-box;
		padding: 0 16px;
	}

	.form-item-title {
		font-size: 18px;
		color: #122d4d;
		margin-bottom: 10px;
	}

	.icon-button {
		margin: 0;

		border-radius: 10px;
		background-color: $app-color-primary;

		&.large {
			width: 52px !important;
			height: 52px !important;
		}

		&.base {
			width: 33px !important;
			height: 33px !important;
		}

		&.flat {
			background-color: #e5f0ff !important;

			.u-icon__icon {
				color: #fff !important;
			}

			&.selected {
				background-color: #e5f0ff !important;

				.u-icon__icon {
					color: $app-color-primary !important;
				}
			}
		}
	}

	.primary-icon {
		margin: 10px;

		.u-icon__icon {
			color: $app-color-primary !important;
		}
	}

	.mui-link {
		color: $app-color-primary;
	}

	.page-title {
		margin-bottom: 20px;
		font-weight: 500;

		&.center {
			text-align: center;
		}
	}

	.section-title {
		font-size: 20px;
		font-weight: 500;
		color: #333333;
		margin-bottom: 10px;

		&.center {
			text-align: center;
		}
	}

	.h1 {
		font-size: 32px;
		line-height: 40px;
		color: #000;
	}

	.h2 {
		font-size: 18px;
		color: #333;
	}

	.h3 {
		font-size: 14px;
		color: #666;
	}

	.h4 {
		font-size: 12px;
		color: #999;
	}

	.uni-list-item__container {
		align-items: center;
	}
</style>